package com.etouch.security.controller;

import com.etouch.security.exception.ExceptionEnum;
import com.etouch.security.exception.ProjectException;
import com.etouch.security.pojo.dto.SysUserDTO;
import com.etouch.security.service.SysUserService;
import com.etouch.security.util.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenyunchang
 * @title
 * @date 2020/10/28 11:10
 * @Description:
 */
@RestController
@RequestMapping("sys")
@Api(tags = "登陆相关接口")
public class SysLoginController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private AuthenticationManager authenticationManager;


    @ApiOperation("用户名,密码登陆")
    @PostMapping("/login")
    public ResultUtils login(String username, String password, HttpServletRequest request) {
        SysUserDTO sysUserDTO = sysUserService.getUserByUserName(username);
        if (sysUserDTO == null) {
            throw new ProjectException(ExceptionEnum.USER_NOT_FOUND);
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(password, sysUserDTO.getPassword());
        if (!matches) {
            throw new ProjectException(ExceptionEnum.USERNAME_OR_PASSWORD_ERRO);
        }
        // 系统登录认证
        return ResultUtils.success();
    }
}
